﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="Common.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：Common.cs
// 创建时间：2015-01-04 20:59
// 修改时间：2015-01-04 20:59
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;

namespace LsTool
{
    /// <summary>
    /// 静态通用
    /// </summary>
    public class ToCom
    {

       
        /// <summary>
        /// 获取拼音全部
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToPinyin(string str)
        {
            string r = string.Empty;
            foreach (var obj in str)
            {
                try
                {
                    var chineseChar = new ChineseChar(obj);
                    string t = chineseChar.ToString();
                    r += t.Substring(0, t.Length - 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }


        /// <summary>
        /// 获取拼音首字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToFirstPinyin(string str)
        {
            string r = string.Empty;
            foreach (char obj in str)
            {
                try
                {
                    var chineseChar = new ChineseChar(obj);
                    string t = chineseChar.ToString();
                    r += t.Substring(0, 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r;
        }



        #region Public Methods and Operators

        /// <summary>
        /// 对所有待签名参数按照字段名的ASCII 码从小到大排序（字典序）后，使用URL键值对的格式（即key1=value1& key2=value2…）拼接成字符串string。
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string ToOrderBy(IEnumerable<KeyValuePair<string, string>> dictionary)
        {
            var dy = dictionary.OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);

            List<string> strings = new List<string>();
            foreach (var item in dy)
            {
                strings.Add(item.Key + "=" + item.Value);
            }

            string join = string.Join("&", strings);

            return join;
        }






 


        /// <summary>
        /// Base64ToString
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static byte[]  Base64ToImg(string txt)
        {
            try
            { 
                return Convert.FromBase64String(txt);
            }
            catch (Exception)
            {
                return new byte[0];
            }
        }
        /// <summary>
        /// Base64ToString
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ImgToBase64(string path)
        {
            try
            {
                var byteArray = File.ReadAllBytes(path);
                return Convert.ToBase64String(byteArray);
            }
            catch (Exception)
            {
                return String.Empty;
            }
        }

        /// <summary>
        /// Base64ToString
        /// </summary>
        /// <param name="imgByte"></param>
        /// <returns></returns>
        public static string ImgToBase64(byte[] imgByte)
        {
            try
            { 
                return Convert.ToBase64String(imgByte);
            }
            catch (Exception)
            {
                return String.Empty;
            }
        }

        /// <summary>
        /// Base64ToString
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static string Base64ToString(string txt)
        {
            try
            {
                byte[] bpath = Convert.FromBase64String(txt);
                return Encoding.UTF8.GetString(bpath);
            }
            catch (Exception)
            { 
                return String.Empty;
            } 
        }
        /// <summary>
        /// StringToBase64
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static string StringToBase64(string txt)
        {
            try
            {
                byte[] bytedata = Encoding.UTF8.GetBytes(txt);
                return Convert.ToBase64String(bytedata, 0, bytedata.Length);
            }
            catch (Exception)
            { 
                return String.Empty; 
            } 
        }



       

        #endregion

    }
}